package com.laizhenghua.wxstub.controller;

import com.alibaba.fastjson.JSONObject;
import com.laizhenghua.wxstub.dto.WxUserDTO;
import com.laizhenghua.wxstub.service.AssistantService;
import com.laizhenghua.wxstub.utils.R;
import com.mongodb.client.result.DeleteResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: AssistantController
 * @author: laizhenghua
 * @date: 2022/8/30 12:39
 */
@Api(tags = "APP-消息推送")
@RestController
@RequestMapping(RestURL.ASSISTANT_URL)
public class AssistantController {

    @Autowired
    private AssistantService assistantService;

    @Scheduled(cron = "0 04 08 * * ?") // 每天08:04执行
    @ApiOperation(value = "推送：每天定时推送微信消息", notes = "api/assistant/push/message", response = JSONObject.class)
    @RequestMapping(value = "/push/message", method = RequestMethod.GET)
    public R pushMessage() {
        return R.ok().put("data", assistantService.pushMessage());
    }

    @ApiOperation(value = "添加：添加发送微信用户", notes = "api/assistant/add/user", response = WxUserDTO.class)
    @RequestMapping(value = "/add/user", method = RequestMethod.POST)
    public R addWxUser(@ApiParam(value = "微信ID(关注公众号获取)", required = true) @RequestParam("wxId") String wxId,
            @ApiParam(value = "微信昵称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "提醒城市名称(用于获取天气信息)", required = true) @RequestParam("city") String city,
            @ApiParam(value = "重要日期如生日(name/MM-dd)", required = true) @RequestParam("date") String date,
            @ApiParam(value = "消息发送模板ID", required = true) @RequestParam("templateId") String templateId,
            @ApiParam(value = "计数器(恋爱天数等每天自动加1)", required = true) @RequestParam("count") Integer count,
            @ApiParam(value = "登录密码(登录本系统密码)", required = true) @RequestParam("password") String password) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(new Date());
        String[] dateSplit = date.split("/");
        String[] split = dateSplit[1].split("-");
        String dateStr = String.format("%s-%s-%s", year, split[0], split[1]);
        SimpleDateFormat parseSdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            parseSdf.parse(dateStr);
        } catch (Exception e) {
            return R.error(500, e.getMessage());
        }
        WxUserDTO wxUser = assistantService.addWxUser(wxId, name, city, date, templateId, count, password);
        return R.ok().put("data", wxUser);
    }

    @ApiOperation(value = "获取：获取发送微信用户列表", notes = "api/assistant/wx/userList", response = WxUserDTO.class)
    @RequestMapping(value = "/wx/userList", method = RequestMethod.GET)
    public R getWxUserList(@ApiParam("分页大小") @RequestParam(value = "pageSize", required = false) Integer pageSize,
                           @ApiParam("页数") @RequestParam(value = "pageNo", required = false) Integer pageNo) {
        return R.ok().put("data", assistantService.getWxUserList(pageSize, pageNo));
    }

    @ApiOperation(value = "删除：删除微信用户", notes = "api/assistant/delete/user", response = DeleteResult.class)
    @RequestMapping(value = "/delete/user", method = RequestMethod.GET)
    public R deleteWxUser(@ApiParam("_id") @RequestParam(value = "_id", required = false) String _id) {
        return R.ok().put("data", assistantService.deleteWxUser(_id));
    }
}
